/*
 * Created on 2005-06-29
 */
package test.serialization;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

public class testSerialization{
    public static final int tests=5;
    public static final int count=500000;
    
    public void testserialization() throws Exception {
        Employee[] emps = new Employee[count];
        for (int n=0; n<count; n++) {
            emps[n] = new Employee("LastName" + n, "FirstName" + n,
                                    "222-33-" + n, 34000 + n,
                                    n % 10);
        }
        for (int outer=0; outer<tests; outer++) {
            ObjectOutputStream oos = null;
            FileOutputStream fos = null;
            BufferedOutputStream bos = null;
            long start = System.currentTimeMillis();
            try {
                fos = new FileOutputStream("TestSerialization");
                bos = new BufferedOutputStream(fos);
                oos = new ObjectOutputStream(bos);
                for (int n=0; n<count; n++) {
                    oos.writeObject(emps[n]);
                }
                long end = System.currentTimeMillis();
                System.out.println("Serialization of " + count + " objects took " + (end-start) + " ms.");
            }
            finally {
                if (oos != null) oos.close();
                if (bos != null) bos.close();
                if (fos != null) fos.close();
            }
            new File("TestSerialization").delete();
        }
    }
    
    public void testserialization2() throws Exception {
        Employee2[] emps = new Employee2[count];
        for (int n=0; n<count; n++) {
            emps[n] = new Employee2("LastName" + n, "FirstName" + n,
                                    "222-33-" + n, 34000 + n,
                                    n % 10);
        }
        for (int outer=0; outer<tests; outer++) {
            ObjectOutputStream oos = null;
            FileOutputStream fos = null;
            BufferedOutputStream bos = null;
            long start = System.currentTimeMillis();
            try {
                fos = new FileOutputStream("TestSerialization");
                bos = new BufferedOutputStream(fos);
                oos = new ObjectOutputStream(bos);
                for (int n=0; n<count; n++) {
                    oos.writeObject(emps[n]);
                }
                long end = System.currentTimeMillis();
                System.out.println("Serialization of " + count + " objects took " + (end-start) + " ms.");
            }
            finally {
                if (oos != null) oos.close();
                if (bos != null) bos.close();
                if (fos != null) fos.close();
            }
            new File("TestSerialization").delete();
        }
    }
}

